<?php
$sanitize_all_escapes = true;
$fake_register_globals = false;

$ignoreAuth = true;
include_once('../interface/globals.php');
require_once("$srcdir/patient.inc");
require_once("history.inc.php");
require_once("$srcdir/acl.inc");
require_once("$srcdir/options.inc.php");

$CPR = 4; // cells per row
////continue session
//session_start();
// kick out if patient not authenticated
$pid = $_REQUEST['pid'];
//echo "pid:" . $pid;
$tmp = getPatientData($pid, "squad");

$medico = "";
$medicoIni = "";
if (isset($_REQUEST['medico']) || isset($_SESSION['medico'])) {
    $medico = "&medico=1&set_pid=" . $pid;
    $medicoIni = "?medico=1&set_pid=" . $pid;
}
?>
<html>
    <head>
        <style>

            #tabs{

            }

            #tabs .ui-tabs .ui-tabs-panel
            {
                background: none !important;
                border: none !important;
                display: block;
                min-height: 110px;
                padding: 20px 20px 40px;
                position: relative;
            }

            #tabs .ui-tabs-active {
                background: #43ACA4;
                color: #ffffff;
            }

            #tabs .ui-widget-header{
                background: none !important;
                border: 0px !important;
            }
            div.section {
                border: solid;
                border-width: 1px;
                border-color: #0000ff;
                margin: 0 0 0 10pt;
                padding: 5pt;
            }

            #demographics_form .text > input {
                font-size: 14px;
                margin-right: 5px;
                padding: 3px;
                width: 175px;
            }

            #demographics_form .label {
                font-size: 14px;
                margin-right: 5px;
            }

        </style> 
        <link rel="stylesheet" href="<?php echo $web_root; ?>/library/js/ui-lightness/jquery-ui-1.10.3.custom.min.css" type="text/css"/>
        <link rel="stylesheet" href="<?php echo $web_root; ?>/patients/css/nightingale.css" type="text/css"/>
        <link rel="stylesheet" type="text/css" href="<?php echo $web_root; ?>/interface/themes/jquery.fancybox.css?v=2.1.5" media="screen"/>

        <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery-1.10.0.min.js"></script>        
        <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery-ui-1.10.3.custom.min2.js"></script>

        <script type="text/javascript" src="<?php echo $web_root; ?>/library/dialog.js"></script>
        <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery.fancybox.pack.js"></script>
        <script type="text/javascript" src="<?php echo $web_root; ?>/patients/js/jquery.mask.min.js"></script>

        <script LANGUAGE="JavaScript">

            var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';

            function divclick(cb, divid) {
                var divstyle = document.getElementById(divid).style;
                if (cb.checked) {
                    divstyle.display = 'block';
                } else {
                    divstyle.display = 'none';
                }
                return true;
            }

            // Compute the length of a string without leading and trailing spaces.
            function trimlen(s) {
                var i = 0;
                var j = s.length - 1;
                for (; i <= j && s.charAt(i) == ' '; ++i)
                    ;
                for (; i <= j && s.charAt(j) == ' '; --j)
                    ;
                if (i > j)
                    return 0;
                return j + 1 - i;
            }

            function validate(f) {
                <?php generate_layout_validation('HIS'); ?>
                return true;
            }

            function submitme() {
                var f = document.forms[0];
                if (validate(f)) {
                    top.restoreSession();
                    f.submit();
                }
            }

            function submit_history() {
                document.forms[0].submit();
            }

            //function for selecting the smoking status in radio button based on the selection of drop down list.
            function radioChange(rbutton)
            {
                if (rbutton == 1 || rbutton == 2)
                {
                    document.getElementById('radio_tobacco[current]').checked = true;
                }
                else if (rbutton == 3)
                {
                    document.getElementById('radio_tobacco[quit]').checked = true;
                }
                else if (rbutton == 4)
                {
                    document.getElementById('radio_tobacco[never]').checked = true;
                }
                else if (rbutton == 5 || rbutton == 9)
                {
                    document.getElementById('radio_tobacco[not_applicable]').checked = true;
                }
                else if (rbutton == '')
                {
                    var radList = document.getElementsByName('radio_tobacco');
                    for (var i = 0; i < radList.length; i++) {
                        if (radList[i].checked)
                            radList[i].checked = false;
                    }
                }
            }

            //function for selecting the smoking status in drop down list based on the selection in radio button.
            function smoking_statusClicked(cb)
            {
                if (cb.value == 'currenttobacco')
                {
                    document.getElementById('form_tobacco').selectedIndex = 1;
                }
                else if (cb.value == 'nevertobacco')
                {
                    document.getElementById('form_tobacco').selectedIndex = 4;
                }
                else if (cb.value == 'quittobacco')
                {
                    document.getElementById('form_tobacco').selectedIndex = 3;
                }
                else if (cb.value == 'not_applicabletobacco')
                {
                    document.getElementById('form_tobacco').selectedIndex = 6;
                }
            }

            /// todo, move this to a common library
            $(document).ready(function() {
                $("#form_DOB").mask("99-99-9999");
                $('#tabs').tabs();
                $("#tabs").css("display", "block");

                $(".ui-datepicker-trigger").after(" <span>(dd-mm-aaaa)</span> ");
                /*tabbify();*/
                setTimeout(function() {
                    $("#mensajeGuardar").hide();
                }, 2000);
                
                var tmp = $("#tabNumber").val();
                
                if(tmp!=''){
                     $("#"+tmp).click();
                }
                                
                $("#div_t1").click(function() {
                    $("#tabNumber").val("div_t1");
                });
                
                $("#div_t2").click(function() {
                    $("#tabNumber").val("div_t2");
                });
                
                $("#div_t3").click(function() {
                    $("#tabNumber").val("div_t3");
                });
                
                $("#div_t0").click(function() {
                    $("#tabNumber").val("div_t0");
                });                

                $("#cancel").click(function() {
                    parent.jQuery.fancybox.close();
                });

            });
        </script>

        <style type="text/css">
            div.tab {
                height: auto;
                width: auto;
            }
        </style>

    </head>
    <body class="body_top_historial">

        <?php
        $result = getHistoryData($pid);
        if (!is_array($result)) {
            newHistoryData($pid);
            $result = getHistoryData($pid);
        }

        $fres = sqlStatement("SELECT * FROM layout_options " .
                "WHERE form_id = 'HIS' AND uor > 0 " .
                "ORDER BY group_name, seq");
        ?>

        <form action="history_save.php<?php echo $medicoIni; ?>" name='history_form' method='post' onsubmit='return validate(this)'>

            <input type='hidden' name='mode' value='save'>
            <?php
            if (isset($_REQUEST['medico'])) {
                ?>
                <input type='hidden' name='medico' value='<?php echo $_REQUEST['medico'] ?>'>
                <?php
            } else if (isset($_SESSION['medico'])) {
                ?>
                <input type='hidden' name='medico' value='<?php echo $_SESSION['medico'] ?>'>
            <?php } ?>
            <input type='hidden' name='pid' value='<?php echo $_SESSION['pid'] ?>'>
            
            <input type='hidden' id="tabNumber" name='tabNumber' value='<?php echo $_REQUEST['tabNumber'] ?>'>
            
            <div class="borde_normal">
                <span class="historial_paciente"><i style="font-size: 1.4em;" class="icon-file-text-alt"></i> <?php echo htmlspecialchars(xl('Patient History / Lifestyle'), ENT_NOQUOTES); ?></span>

                <?php if(!acl_check('admin', 'adm_cnf')){ ?>
                    <a href="javascript:submit_history();" id="ok" class="css_button"><span><i class="icon-ok-sign" style="font-size: 25px;"></i> <?php echo htmlspecialchars(xl('Save'), ENT_NOQUOTES); ?></span></a>
                <?php } ?>    
                <a href="" id="cancel" class="css_button"><span><i class="icon-remove-sign" style="font-size: 25px;"></i> <?php xl('Cancel', 'e'); ?></span></a>

            </div>

            <div id="mensajeGuardar" class="historial_paciente" <?php if (isset($mensajeGuardar)) echo "style='display:block;'"; ?>>
                <?php
                if (isset($mensajeGuardar))
                    echo "<span>El formulario se ha guardado correctamente.</span>"
                    ?>
            </div>
            <!-- history tabs -->
            <div id="tabs" style="display: none;">
                <?php
                $group_seq = 0; // this gives the DIV blocks unique IDs
                ?>

                <?php
                echo "<ul>";
                display_layout_tabs('HIS', $result, $result2);
                echo "</ul>";
                ?>
                <?php display_layout_tabs_data_editable('HIS', $result, $result2); ?>

        </form>

        <!-- include support for the list-add selectbox feature -->
        <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>

    </body>

    <script language="JavaScript">
<?php //echo $date_init; // setup for popup calendars          ?>
    </script>

</html>
